package com.myhkvideo.yulongbpcdemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginService {

    private static final String GET_VERIFY_CODE_URL = "http://ylty.westmining.com:60016/User/GetVerifyCode";
    private static final String LOGIN_URL = "http://ylty.westmining.com:60016/User/Login";
    private static final String USERNAME = "yxn";
    private static final String PASSWORD = "Software@02";

    @Autowired
    @Qualifier("RestTemplateConfig")
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    public String getNewToken() {
        // Step 1: Get verify code
        ResponseEntity<String> verifyCodeResponse = restTemplate.exchange(GET_VERIFY_CODE_URL, HttpMethod.GET, null, String.class);
        try {
            Map<String, Object> verifyCodeBody = objectMapper.readValue(verifyCodeResponse.getBody(), Map.class);
            if (verifyCodeBody != null && (boolean) verifyCodeBody.get("Code")) {
                Object dataObj = verifyCodeBody.get("Data");
                Map<String, String> verifyCodeData = null;
                if (dataObj instanceof java.util.ArrayList) {
                    java.util.ArrayList<?> dataList = (java.util.ArrayList<?>) dataObj;
                    if (!dataList.isEmpty() && dataList.get(0) instanceof Map) {
                        verifyCodeData = (Map<String, String>) dataList.get(0);
                    }
                } else if (dataObj instanceof Map) {
                    verifyCodeData = (Map<String, String>) dataObj;
                }

                if (verifyCodeData != null) {
                    String indeCode = verifyCodeData.get("indeCode");
                    String code = verifyCodeData.get("Code");

                    // Step 2: Login with verify code
                    Map<String, String> loginRequest = new HashMap<>();
                    loginRequest.put("indeCode", indeCode);
                    loginRequest.put("code", code);
                    loginRequest.put("userName", USERNAME);
                    loginRequest.put("password", PASSWORD);

                    HttpHeaders headers = new HttpHeaders();
                    headers.add("Content-Type", "application/json");
                    HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(loginRequest, headers);

                    ResponseEntity<String> loginResponse = restTemplate.exchange(LOGIN_URL, HttpMethod.POST, requestEntity, String.class);
                    Map<String, Object> loginBody = objectMapper.readValue(loginResponse.getBody(), Map.class);
                    if (loginBody != null && (boolean) loginBody.get("Code")) {
                        Map<String, Object> loginData = (Map<String, Object>) loginBody.get("Data");
                        return (String) loginData.get("access_token");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}